Ext.ns('TaxonTagger');
TaxonTagger.TaxamatchLookup = function(config){
	
	this.store1 = new Ext.data.JsonStore({
		fields: ['sciname']
	});
		
	this.sm =	new Ext.grid.RowSelectionModel({
			singleSelect: true
		,	listeners: {
				rowselect: function(sm, row, rec){
					this.keyName= rec.data.sciname;
				}
			}
	});

	Ext.apply(this, config, {
			width: 373
		,	height: 241
		,	layout: 'fit'
		,	modal: true
		,	resizable: false
		,	defaults: {
					border: false
				,	xtype: 'button'
			}
		,	plain: true
		,	closable: true		
		,	items: [{
					xtype: 'grid'
				,	ref: 'grid1'	
				,	store: this.store1	
				,	sm: this.sm
				,	loadMask: {msg: 'Loading...'}
				,	columns: [{
							xtype: 'gridcolumn'
						,	id: 'scinameid'	
						,	dataIndex: 'sciname'
						,	header: 'Names'
						,	sortable: true
						,	width: 100
					}]
				,	listeners: {
							rowdblclick: function(grid, rowIndex, e){
								var selText= grid.getStore().getAt(rowIndex).get('sciname');
								this.addSci(selText);
							}
						,	scope: this	
					}
				,	autoExpandColumn: 'scinameid' 
			}]
		,	tbar: {
					xtype: 'toolbar'
				,	items: [{
							xtype: 'tbtext'
						,	text: this.txtTbarSciName
					}, '&nbsp;', {
							xtype: 'textfield'
						,	emptyText: this.txtemptyText
						,	scope: this
						,	ref: '../txtSearch'	
						,	enableKeyEvents: true
						,	listeners: {
								keypress: function(el, e){
									var serachVal= this.txtSearch.getValue();
									if(e.keyCode == 13){
										if(Ext.isEmpty(serachVal)){
											TaxonTagger.Notice.msg(this.msgNoticeTitle, this.msgNoticeSearch);
										} else {
											this.searchSci(serachVal);
										}
									}
								}
							,	scope: this
						}
 	      	}, '&nbsp;', {
							text: this.txtTbarSearch
						,	scope: this
						,	ref: '../btnSearch'
						,	iconCls: 'icon_load'
						,	handler: function(){
								var serachVal= this.txtSearch.getValue();
								if(Ext.isEmpty(serachVal)){
									TaxonTagger.Notice.msg(this.msgNoticeTitle, this.msgNoticeSearch);
								} else {
									this.searchSci(serachVal);
								}
							}	
					}]
	     }
		,	fbar: {
					xtype: 'toolbar'
				,	items: [{
							text: this.btnFbarSciName
						,	scope: this 	
						,	handler: function(){
								var selSciname = this.grid1.selModel.getSelections();
								 if (selSciname.length > 0) {
									this.addSci(selSciname[0].data.sciname)
								} else {
									TaxonTagger.Notice.msg(this.msgNoticeTitle, this.msgNoticeSelect);
								}
							}		 
					}, {
							text: this.btnFbarClose
						,	scope: this
						,	handler: function(){
								this.close();
							}	
					}]
      }
	});
	
	TaxonTagger.TaxamatchLookup.superclass.constructor.call(this, config);
	
};

Ext.extend(TaxonTagger.TaxamatchLookup, Ext.Window, {

		searchSci: function(serachVal){
			this.grid1.loadMask.show();
			Ext.Ajax.request({
					scope: this
				,	url: 'resources/json/tmp.json'
				,	params: {
							sciname: serachVal
						,	authority: 'col2010'
					}
				,	success: function(response){
						var response = Ext.decode(response.responseText);
						this.store1.loadData(response);
					}
				,	failure: function(result){
						TaxonTagger.Notice.msg('Notice', 'Error in loading data.');
					}
			});
			this.grid1.loadMask.hide();	
		}

	,	addSci: function(sciVal){
			var verName = this.keyName
			var f = Ext.getCmp('mifp').getFrameDocument().body;
			Ext.each(Ext.fly(f).select("span.tt-sciname").elements, function(el){
				if(el.getAttribute("verbatim") == verName){
					el.setAttribute("scientificname", sciVal);
				}
			});	
			var str =  String.format('<div>{0} "{1}" {2} </div>', this.msgNoticeSciName, sciVal, this.msgNoticeAssigned);
			TaxonTagger.Notice.msg(this.msgNoticeTitle, str);
			this.close();	
		}

});